import {HTableColumns} from "@/components/ant-ext-table/HTable";
import HTag from "@/components/ant-ext/HTag";
import {AdminStat} from "@/const/const";
import {Form} from "antd";
import HDataTable from "@/components/ant-ext-table/HDataTable";
import {DeleteOutlined, PlusSquareOutlined} from "@ant-design/icons";
import SysAdmin from "@/api/sys-admin";
import AdminEdit from "@/pages/sys/admin/edit";
import {HSearchTree} from "@/components/ant-ext/HSearchTree";
import React, {Key, useState} from "react";
import {useHandle} from "@/common/hooks";
import SysDepart, {SysDepartTree} from "@/api/sys-depart";
import "./index.less"
import {util} from "@/common/util";
import HAction from "@/components/common/HAction";
import {HDataTableSlice} from "@/components/ant-ext-table/redux/dataTableSlice";
import SysRole from "@/api/sys-role";
import {useDispatch} from "react-redux";
import {Dispatch} from "redux";

function getColumns(dispatch: Dispatch): HTableColumns<any> {
    return [
        {
            title: "人员编码",
            dataIndex: 'adminCode',
            width: '200px',
            fixed: 'left',
            render: (value, record) => {
                return <HAction type={"link"}
                                empower={SysAdmin.EDIT}
                                invalid
                                action={() => dispatch!(HDataTableSlice.actions.doAction({
                                    action: SysAdmin.EDIT,
                                    rowKey: SysAdmin.RowKey,
                                    row: record
                                }))}
                                children={value}/>
            }
        }, {
            title: "登录名",
            dataIndex: 'loginName',
        }, {
            title: "人员名称",
            dataIndex: 'adminName',
        }, {
            title: "角色名称",
            dataIndex: 'roleName',
        }, {
            title: "状态",
            dataIndex: 'stat',
            render: (value: any) => <HTag value={value} mapping={AdminStat.tag}/>
        }, {
            title: "隶属组织",
            dataIndex: 'orgNamePath',
        }, {
            title: "修改人",
            dataIndex: 'updateBy',
        }, {
            title: "修改时间",
            dataIndex: 'updateTime',
        }, {
            title: "创建人",
            dataIndex: 'createBy',
        }, {
            title: "创建时间",
            dataIndex: 'createTime',
        }
    ]
}

export default function Index() {
    const [form] = Form.useForm()
    const [treeData, setTreeData] = useState<SysDepartTree[]>([]);
    const [loading, setLoading] = useState(false);
    const [refreshExpandedKeys, setRefreshExpandedKeys] = useState<Key[]>([])
    const dispatch = useDispatch()


    const reload = useHandle(() => SysDepart.treeView({
        setLoading,
        success: (data => {
            setTreeData(data)
            let keys: Key[] = []
            // util.getTreeAllKey(keys, data)
            setRefreshExpandedKeys(keys)
        })
    }), [])
    return <>
        <AdminEdit/>
        <div className="parent">
            <div className={"searchSelect"}>
                {/* <HSearchTree blockNode showLine showIcon={false} treeData={treeData} reload={reload} loading={loading}
                             refreshExpandedKeys={refreshExpandedKeys}
                             onSelect={(keys) => dispatch(HDataTableSlice.actions.setQueryParams({orgCode: keys[0]}))}/> */}

            </div>
            <div className={"dataTable"}>
                <HDataTable
                    option={[
                        {key: SysAdmin.ADD, title: "新增", icon: <PlusSquareOutlined/>},
                        {
                            key: SysAdmin.DEL,
                            title: "删除",
                            mode: "multi",
                            icon: <DeleteOutlined style={{color: "red"}}/>,
                            confirm: "是否要删除所选数据",
                            action: (data) => {
                                SysAdmin.delete({
                                    body: {code: data.selectRowKeys},
                                    success: () => dispatch(HDataTableSlice.actions.setQueryParams({}))
                                })
                            }
                        },
                    ]}
                    queryForm={{
                        form: form,
                        item: [
                            {name: "adminCode", label: "人员编码"},
                            {name: "adminName", label: "人员名称"},
                            {name: "stat", label: "状态", type: "select", dataSource: AdminStat.select, width: "150px"},
                            {
                                name: "roleCode",
                                label: "角色编码",
                                type: "select",
                                dataSource: SysRole.selectList,
                                width: "200px"
                            },
                        ]
                    }}
                    columns={getColumns(dispatch)} selectMethod={SysAdmin.selectTablePage}
                    rowKey={SysAdmin.RowKey}
                />
            </div>
        </div>
    </>
}